package models

import (
	"database/sql"
	"syncCard/logger"
	"time"

	go_ora "github.com/sijms/go-ora/v2"
)

var lg = logger.InitLog()

func DataRun(OraServer string, OraPort int, OraCid string, username string, password string, rdaccount *RDACCOUNT, readerCardInfo *READER_CARDINFO, reader *READER) error {
	connStr := go_ora.BuildUrl(OraServer, OraPort, OraCid, username, password, nil)
	conn, err := sql.Open("oracle", connStr)
	if err != nil {
		lg.Error("DataRun sql.Open Error:", err)
		return err
	}
	// check for error
	err = conn.Ping()
	// check for error
	if err != nil {
		lg.Error("DataRun Ping Error:", err)
		return err
	}
	defer conn.Close()
	conn.SetConnMaxLifetime(time.Second * 15)
	conn.SetMaxOpenConns(100)
	conn.SetMaxIdleConns(100)
	err = ReadIn(conn, reader)
	if err != nil {
		lg.Error("DataRun ReadIn Error:", err)
		return err
	}

	err = readerCardInfoI(conn, readerCardInfo)
	if err != nil {
		lg.Error("DataRun readerCardInfoI Error:", err)
		return err
	}

	err = RdAccountUin(conn, rdaccount)
	if err != nil {
		lg.Error("DataRun RdAccountUin Error:", err)
		return err
	}
	return nil
}
